package cn.com.egova.erp.action;

import cn.com.egova.erp.biz.IReturnordersBiz;
import cn.com.egova.erp.entity.Emp;
import cn.com.egova.erp.entity.Returnorderdetail;
import cn.com.egova.erp.entity.Returnorders;
import com.alibaba.fastjson.JSON;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 退货订单Action
 *
 * @author Administrator
 */
public class ReturnordersAction extends BaseAction<Returnorders> {

    private IReturnordersBiz returnordersBiz;
    private String json;

    public void setReturnordersBiz(IReturnordersBiz returnordersBiz) {
        this.returnordersBiz = returnordersBiz;
        super.setBaseBiz(this.returnordersBiz);
    }

    public void setJson(String json) {
        this.json = json;
    }

    public String getJson() {
        return json;
    }


    public void myListByPage() {
        Returnorders returnorders = getT1();
        if (returnorders == null) {
            returnorders = new Returnorders();
            setT1(returnorders);
        }
        Emp loginUser = getLoginUser();
        if (loginUser == null) {
            ajaxReturn(false, "请先登录");
            return;
        }
        returnorders.setCreater(loginUser.getUuid());
        super.listByPage();
    }


    @Override
    public void add() {
        Emp loginUser = getLoginUser();
        if (loginUser == null) {
            ajaxReturn(false, "请先登录!");
            return;
        }
        try {
            Returnorders returnorders = getT();
            returnorders.setCreater(loginUser.getUuid());
            List<Returnorderdetail> returnorderdetails = JSON.parseArray(json, Returnorderdetail.class);
            returnorders.setReturnorderdetails(returnorderdetails);
            returnordersBiz.add(returnorders);
            ajaxReturn(true, "新增成功!");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(true, "新增失败!");
        }
    }


    public void doCheck() {
        Emp loginUser = getLoginUser();
        if (loginUser == null) {
            ajaxReturn(false, "请先登录!");
            return;
        }
        try {
            returnordersBiz.doCheck(getId(), loginUser.getUuid());
            ajaxReturn(true, "审核成功!");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(false, "审核失败!");
        }
    }


    /**
     * 导出订单
     */
    public void exportByTemplate() {
        String fileName = "ReturnOrders_" + getId() + ".xls";
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            //设置输出流，实现下载，指定是excel,因为文件名有中文，故还要解决中文乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
            returnordersBiz.exportByTemplate(response.getOutputStream(), getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
